package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/file",headers = "Content-Type= multipart/form-data", method = RequestMethod.POST)
public class FileController {

    @Autowired
    private Category category;
    @Autowired
    private Product product;
    @Autowired
    private CatalogService catalogService;
    @Autowired
    HttpSession session;
    @PostMapping("/fileUpload")
    @ResponseBody
    public List<Category> fileUpload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "categoryId") String categoryId, @RequestParam(value = "name") String name) throws IOException {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"; // 上传后的路径
        //String filePath = "../../../../../resources/static/images";
        String path = "../images/"+fileName;
//        fileName = UUID.randomUUID() + suffixName; // 新文件名
        category.setCategoryId(categoryId);
        category.setName(name);
        category.setDescription(path);
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        List<Category> categoryList;
        file.transferTo(dest);
        catalogService.insertCategory(category.getCategoryId(),category.getName(),category.getDescription());
        categoryList = catalogService.getCategoryList();
        //model.addAttribute("categoryList",categoryList);
        session.setAttribute("categoryList",categoryList);
        return categoryList;
    }

    @PostMapping("/categoryImgEdit")
    @ResponseBody
    public Category categoryImgEdit(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "categoryId") String categoryId, @RequestParam(value = "name") String name)throws IOException {
        Category category = catalogService.getCategory(categoryId);
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"; // 上传后的路径
        //String filePath = "../../../../../resources/static/images";
        String path = "../images/"+fileName;
//        fileName = UUID.randomUUID() + suffixName; // 新文件名
        category.setCategoryId(categoryId);
        category.setName(name);
        category.setDescription(path);
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        catalogService.editCategory(category);

        return category;
    }

    @PostMapping("/productFile")
    @ResponseBody
    public List<Product> productFileUpload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "categoryId") String categoryId,@RequestParam(value = "productId") String productId, @RequestParam(value = "name") String name) throws IOException{
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"; // 上传后的路径
        //String filePath = "../../../../../resources/static/images";
        String path = "../images/"+fileName;
//        fileName = UUID.randomUUID() + suffixName; // 新文件名
        product.setCategoryId(categoryId);
        product.setProductId(productId);
        product.setName(name);
        product.setDescription(path);
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        List<Product> productList;
        file.transferTo(dest);
        catalogService.insertProduct(product);
        productList = catalogService.getProductListByCategory(categoryId);
        //model.addAttribute("categoryList",categoryList);
        session.setAttribute("productList",productList);
        return productList;
    }

    @GetMapping("/productImgEdit")
    @ResponseBody
    public Product productImgEdit(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "productId") String productID,@RequestParam(value = "categoryId") String categoryId, @RequestParam(value = "name") String name) throws IOException{
        Product product = catalogService.getProduct(productID);
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        if(fileName.indexOf("\\") != -1){
            fileName = fileName.substring(fileName.lastIndexOf("\\"));
        }
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\"; // 上传后的路径
        //String filePath = "../../../../../resources/static/images";
        String path = "../images/"+fileName;
//        fileName = UUID.randomUUID() + suffixName; // 新文件名
        System.out.println(productID+" "+categoryId+" "+name);
        product.setProductId(productID);
        product.setCategoryId(categoryId);
        product.setName(name);
        product.setDescription(path);
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        catalogService.editProduct(product);

        return product;
    }

}

